package com.byit.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.byit.builder.MapBuilder;
import com.byit.constant.DefaultValue;
import com.byit.domain.SysDept;
import com.byit.enums.PromptInfo;
import com.byit.msg.MessageResult;
import com.byit.service.ISysDeptService;
import com.byit.shrio.session.SessionHelper;
import com.byit.utils.StringUtil;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author wangx
 * @since 2018-12-11
 */
@RestController
@RequestMapping("/api/sys/sysDept")
public class SysDeptController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISysDeptService deptService;

    @PutMapping("/saveOrUpdate")
    public Object insertDept(HttpSession session, SysDept dept) {
        Long userId = SessionHelper.getUserId(session);
        try {
            if (null == dept) {
                return MessageResult.warpper(PromptInfo.ERROR_PARAM_NOT_NULL);
            }
            if (dept.getId() != 0) {
                dept.setUpdateTime(new Date());
            } else {
                dept.setCreateTime(new Date());
            }
            //// 加密用户密码
            dept.setOperationId(userId);
            deptService.insertOrUpdate(dept);
            return MessageResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return MessageResult.error(e);
        }
    }

    @GetMapping("/selectById/{id}")
    public Object selectDeptById(@PathVariable("id") Long id) {
        SysDept sysDept = deptService.selectById(id);
        return MessageResult.success(sysDept);
    }

    @DeleteMapping("/delDept/{ids}")
    public Object delDeptByIds(HttpSession session, @PathVariable("ids") String ids) {
        Long userId = SessionHelper.getUserId(session);
        List<Long> idList = StringUtil.splitLong(ids, ",");
        Map<String, Object> map = MapBuilder.newMap(3).setParam("updateTime", new Date()).setParam("operId", userId).ok();

        for (Long id : idList) {
            map.put("id", id);
            deptService.delDeptById(map);
        }
        return MessageResult.success();
    }

    @GetMapping("/selectAll")
    public Object selectDeptAll(HttpSession session, @RequestParam(value = "name", defaultValue = "") String name,
                                @RequestParam(value = "page", defaultValue = DefaultValue.PAGE_FIRST) int page,
                                @RequestParam(value = "limit", defaultValue = DefaultValue.PAGE_SIZE) int limit) {
        Page<SysDept> pageInfo = new Page<>(page, limit);
        Map<String, Object> params = new HashedMap();
        if (null != name && !"".equals(name)) {
            params.put("name", "%" + StringUtil.decodeSpecialCharsWhenLikeUseBackslash(name) + "%");
        }
        Page<JSONObject> result = deptService.selectDeptAll(pageInfo, params);
        return MessageResult.warpper(PromptInfo.SYS_SECCUSS, result);
    }

}
